﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace TechControl.BD
{
    class ModeloDB: BaseDB
    {
        private static ModeloDB instance;

        private ModeloDB() { }

        public static ModeloDB getInstance()
        {
            if (instance == null)
            {
                instance = new ModeloDB();
            }
            return instance;
        }

        //retorna modelos de acordo com a marca informada.
        public ArrayList selectByMarca(Int64 marca)
        {
            ArrayList result = new ArrayList();
            try
            {
                this.open();
                SqlCommand command = new SqlCommand("Select id_modelo, modelo from tb_modelo where id_marca = '" + marca + "';", this.conn);
                SqlDataReader rdr = command.ExecuteReader();

                while (rdr.Read())
                {
                    result.Add(rdr.GetInt64(0).ToString() + "= " + rdr.GetString(1));
                }
            }
            finally
            {
                this.close();
            }
            return result;
        }

        //retorna descrição do modelo de acordo com o código informado.
        public string selectDescModelo(Int64 modelo)
        {
            string result = "";
            try
            {
                this.open();
                SqlCommand command = new SqlCommand("select desc_modelo from tb_modelo where id_modelo = '" + modelo + "';", this.conn);
                SqlDataReader rdr = command.ExecuteReader();

                while (rdr.Read())
                {
                    result = rdr.GetString(0);
                }
            }
            catch (Exception) { result = "Não Possuí"; }
            finally { this.close(); }
            
            return result;
        }

        //retonra número do ultimo registro na tabela TB_modelo.
        public Int64 getLastCodModelo()
        {
            Int64 result = 0;
            try
            {
                this.open();
                SqlCommand command = new SqlCommand("Select MAX(id_modelo) from tb_modelo;",this.conn);
                SqlDataReader rdr = command.ExecuteReader();
                while (rdr.Read())
                {
                    result = rdr.GetInt64(0);
                }
            }
            catch (Exception) { result = 0; }
            finally { this.close(); }
            return result;
        }

        //inseri novo registro.
        public bool insertNewModelo(Int64 idModelo, Int64 idMarca, string modelo, string descModelo)
        {
            bool result = true;
            try
            {
                this.open();
                SqlCommand command = new SqlCommand("Insert into tb_modelo values('" + idModelo + "', '" + idMarca + "', '" + modelo +
                    "', '" + descModelo + "');", this.conn);
                command.ExecuteNonQuery();
            }
            catch (Exception) { result = false; }
            finally { this.close(); }

            return result;
        }

        //retorna todas od modelos na base.
        public ArrayList selectAllModelos()
        {
            ArrayList result = new ArrayList();
            try
            {
                this.open();
                SqlCommand command = new SqlCommand("select * from tb_modelo;", this.conn);
                SqlDataReader rdr = command.ExecuteReader();

                while (rdr.Read())
                {
                    result.Add(rdr.GetInt64(0).ToString() + "= " + rdr.GetString(2));
                }
            }
            finally
            {
                this.close();
            }
            return result;
        }

    }
}
